var page_mask = document.getElementById("ui-view-29");   			//防弹层
var paybar = document.getElementById("js_paybar_wrap");               //支付栏
var paybar_prices = paybar.getElementsByClassName("pay-inblock")[0];
var paybar_price = paybar_prices.getElementsByTagName("b")[0];         //支付栏支付金额
var initial_price = parseInt($("#initial_price").html()); //初始价格
var paybar_pay = paybar.getElementsByClassName("pay-submit js_booking_paybtn")[0]; //支付按钮
var room_price = parseInt(paybar_price.innerHTML);                     //每间房的价格
var msg = document.getElementById("ui-view-13");                      //信息修改页面
var msg_intime = msg.getElementsByClassName("bk-fn17 js_inday_main_title")[0];//信息修改入住日期
var msg_intime_xq = msg.getElementsByClassName("bk-color5 js_inday_sub_title")[0];//信息修改入住星期
var msg_outtime = msg.getElementsByClassName("bk-color5 js_outday_sub_title")[0];//信息修改离店日期
var msg_tip = document.getElementById("ui-view-17")	                //信息修改提示页面
var msg_tip_price = msg_tip.getElementsByClassName("bk-color5")[0];   //信息修改价格提示
var msg_tip_ok = msg_tip.getElementsByClassName("cui-flexbd cui-btns-ok")[0];//信息修改提示确定按钮
var change_msg = document.getElementsByClassName("bk-color2")[0];     //修改信息按钮
var user_konw = document.getElementById("js_cancelPolicyInfo");       //用户须知按钮
var user_page = document.getElementById("ui-view-23");                //用户须知页面
var close_user_page = document.getElementsByClassName("font-g-close")[0];//关闭用户页面
var user_page_mask = document.getElementsByClassName("scroll-mask-layer js_scroll_mask animated animated1 fadeIn")[0];
var drop_btn = document.getElementById("ui-view-6"); 					//下拉按钮
var drop_room = document.getElementById("ui-view-7"); 			    //下拉房间
var exit_page_btn = document.getElementsByClassName("cm-header-icon fl js_back")[0];  //推出页面按钮
var exit_page = document.getElementById("ui-view-11");						//推出提示页面
var continue_btn = document.getElementsByClassName("cui-flexbd cui-btns-cancel")[0]; //继续按钮
var exit_btn = document.getElementsByClassName("cui-flexbd cui-btns-ok")[0];    //离开按钮
var off = false;
var rooms = 1;   //默认房间数是1
var show_day = document.getElementsByClassName("bk-fn17 js_outday_main_title")[0];//显示位置
var add_day = document.getElementsByClassName("cm-adjust-plus js_num_plus ")[0]; //添加按钮
var cut_day = document.getElementsByClassName("cm-adjust-minus js_num_minus  disabled  ")[0];//减少按钮
var decide_day_btn = document.getElementsByClassName("io-btn js_submit")[0];   //确定天数按钮
var day_msg = document.getElementById("js_checkInOutDay");           //住房信息
var day_msg_detail = day_msg.querySelectorAll("em");				 //订单具体信息
var room_num = document.getElementsByClassName("m-txt")[0];          //显示房间数量
var room_control = document.getElementById("ui-view-7");             //下拉控件
var room_control_num = room_control.querySelectorAll("span");

var name_box = document.getElementById("ui-view-8");  			//用户姓名栏
var name_box_ul = $(".js_passenger_list"); 						//ul栏
var coupon = document.getElementById("js_booking_discount");    //优惠券栏

var detail_money_btn = paybar.getElementsByClassName("pay-detail js_price_detail_link")[0];  //费用详细按钮
var detail_money_page = document.getElementById("detail_feiyong"); 						     //费用明细页面


var coupon_content = document.getElementById("ui-view-12");   				          //优惠券内容
var no_ul = coupon_content.getElementsByClassName("vou-container bk-t-border")[0];    //不用优惠券
var no_ul_li = coupon_content.getElementsByTagName("li")[0];
var no_ul_span = no_ul.getElementsByTagName("span")[0];							      //不用优惠券的按钮
var use_ul = coupon_content.getElementsByClassName("vou-container mt5")[0];           //可用优惠券
var coupon_list = use_ul.getElementsByTagName("li");  				                  //可用优惠券列表
var coupon_list_btn = [];
var coupon_btn_span = [];
var discount_moneys = [];
var discount_money = [];   //优惠券的金额
var money_num = [];

var coupon_text = coupon.getElementsByClassName("m-txt m-txt-space js_discount_info btn btn-block")[0];  //优惠券文字描述

//获取首页传过来的数据
/*var str=window.location.search;*/ //从？开始后面的字符串
// var str=self.location.href;     //获取上面传来的参数
// var p=decodeURI(str);   	//解码
// var s=p.split("?");
// var get_date=JSON.parse(s[1])[0];  //获取传过来的日期
// var get_intime=get_date.intime;    //入住日期
// var get_outtime=get_date.outtime;	//离店日期


var get_intime = getQueryString("indate");
var get_outtime = getQueryString("outdate");
var get_between = daysBetween(get_intime, get_outtime); //日期差


//获取url参数
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var reg_rewrite = new RegExp("(^|/)" + name + "/([^/]*)(/|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    var q = window.location.pathname.substr(1).match(reg_rewrite);
    if (r != null) {
        return unescape(r[2]);
    } else if (q != null) {
        return unescape(q[2]);
    } else {
        return null;
    }
}

//house_id
var house_id = getQueryString('house_id');

//不用优惠券
function no_coupon() {
    coupon_text.innerHTML = "不使用";

    $("#js_paybar_wrap").find("p").eq(1).css("display", "none");
    $("#detail_feiyong").find(".no3").eq(2).text("");
    init();
}

//初始化支付页面
init();

function init() {
    var i = get_intime.split("/");
    var o = get_outtime.split("/");

    function enter_date(i) {
        return "入住：" + i[1] + "月" + i[2] + "日";
    }

    function exit_date(o) {
        return "离店：" + o[1] + "月" + o[2] + "日";
    }

    day_msg_detail[0].innerHTML = enter_date(i);
    day_msg_detail[1].innerHTML = exit_date(o);
    day_msg_detail[2].innerHTML = get_between + "晚";
    show_day.innerHTML = "住" + get_between + "晚";
    paybar_price.innerHTML = room_price * get_between * rooms;   //金额
    console.log(paybar_price);
    msg_intime.innerHTML = i[1] + "月" + i[2] + "日";
    msg_intime_xq.innerHTML = "(" + xq(get_intime) + ")";  //入住星期
    msg_outtime.innerHTML = o[1] + "月" + o[2] + "日" + "(" + xq(get_outtime) + ")";
    final_money = room_price * get_between * rooms;
    all_money =initial_price * get_between * rooms;

}
/*关闭所有副业面*/
function close_all() {
    page_mask.style.display = "none";
    msg.style.display = "none";
    exit_page.style.display = "none";
    msg_tip.style.display = "none";
}

/*点击防弹层*/
page_mask.onclick = function () {
    close_all();
};

/*姓名li_html*/
function create_li_html(index) {
    var str = '<li data-index=' + index + '>' +
        '<input type="text" class="m-input nofastclick js_input" name="in_person[]" placeholder="姓名，每间只需填写1人" data-ubt-key="c_hotel_booking_psg_cname" >' +
        '<span data-index="clearUser" class="bk-right-focus js_close_icon" style="display: none;" data-ubt-key="c_hotel_booking_psg_clear_cname">' +
        '<i class="bk-icon-close"></i>' +
        '</span>' +
        '</li>';
    return str;
}

/*动态创建用户姓名栏*/
function create_name(n) {
    name_box_ul.html(null); //清空
    for (var i = 0; i < n; i++) {
        var s = create_li_html(i);
        name_box_ul.append(s);
    }
}

/*下拉房间*/
drop_btn.onclick = function () {
    if (off) {
        drop_room.style.display = "none";
        off = !off
    } else {
        drop_room.style.display = "";
        off = !off;
    }
    for (var i = 0; i < room_control_num.length; i++) {
        room_control_num[i].index = i + 1;
        room_control_num[i].onclick = function () {
            room_num.innerHTML = this.index + "间";
            rooms = this.index;
            console.log(rooms);
            paybar_price.innerHTML = room_price * get_between * rooms;
            final_money = room_price * get_between * rooms;
            all_money =initial_price * get_between * rooms;
            for (var i = 0; i < room_control_num.length; i++) {
                room_control_num[i].className = "item";
            }
            this.className = "item current";
            drop_room.style.display = "none";
            off = !off;
            create_name(rooms);
            no_coupon();
        }
    }
}

//修改信息
change_msg.onclick = function () {
    msg.style.display = "block";
    page_mask.style.display = "block";
    //添加天数
    add_day.onclick = function () {
        get_between++;
        var s = next_days(get_intime, get_between);  //取得添加天数后的日期
        get_outtime = s;
        var n = s.split("/");
        show_day.innerHTML = "住" + get_between + "晚";
        if (n[1].length == 1) {
            msg_outtime.innerHTML = "0" + n[1] + "月" + n[2] + "日" + "(" + xq(next_days(get_intime, get_between)) + ")";
        } else {
            msg_outtime.innerHTML = n[1] + "月" + n[2] + "日" + "(" + xq(next_days(get_intime, get_between)) + ")";
        }
    }
    //减少天数
    cut_day.onclick = function () {
        if (get_between == 1) {
            return;
        }
        get_between--;
        var s = next_days(get_intime, get_between);  //取得减少天数后的日期
        get_outtime = s;
        var n = s.split("/");
        show_day.innerHTML = "住" + get_between + "晚";
        if (n[1].length == 1) {
            msg_outtime.innerHTML = "0" + n[1] + "月" + n[2] + "日" + "(" + xq(next_days(get_intime, get_between)) + ")";
        } else {
            msg_outtime.innerHTML = n[1] + "月" + n[2] + "日" + "(" + xq(next_days(get_intime, get_between)) + ")";
        }

    }

    //确定修改
    decide_day_btn.onclick = function () {
        day_msg_detail[2].innerHTML = get_between + "晚";
        msg.style.display = "none";
        //入住晚数改变提示
        msg_tip.style.display = "block";
        page_mask.style.display = "block";

        var s = next_days(get_intime, get_between);  //改变天数后的日期
        change_data = s;
        var n = s.split("/");
        if (n[1].length == 1) {
            day_msg_detail[1].innerHTML = "离店：" + "0" + n[1] + "月" + n[2] + "日";
        } else {
            day_msg_detail[1].innerHTML = "离店：" + n[1] + "月" + n[2] + "日";
        }
        var money = room_price * get_between * rooms;
        paybar_price.innerHTML = money;                  //支付栏的金额
        msg_tip_price.innerHTML = "￥" + money;              //信息提示的金额
        final_money = room_price * get_between * rooms;
        all_money =initial_price * get_between * rooms ;
        no_coupon();
        msg_tip_ok.onclick = function () {
            close_all();
        }
    }
}

//用户须知
user_konw.onclick = function () {
    user_page.style.display = "block";
    /*关闭用户须知*/
    close_user_page.onclick = function () {
        user_page.style.display = "none";
    }
    user_page_mask.onclick = function () {
        user_page.style.display = "none";
    }
}

/*退出页面提示*/
exit_page_btn.onclick = function () {
    page_mask.style.display = "block";
    exit_page.style.display = "block";
    continue_btn.onclick = function () {
        close_all();
    }
};

var redpack_money = "";

//判断有没有券
if(coupon_list.length == 0){
    coupon_content.getElementsByClassName("plr10 mt5")[0].style.display="none";
}else{
    //选择可用优惠券
    for(var i=0;i<coupon_list.length;i++){
        coupon_list_btn[i] = coupon_list[i].getElementsByClassName("vou-select")[0];      //优惠券按钮
        coupon_btn_span[i] = coupon_list_btn[i].getElementsByTagName("span")[0];          //选中按钮
        discount_moneys[i] = coupon_list[i].getElementsByClassName("vou-cards")[0];
        discount_money[i] = discount_moneys[i].innerText;                                 //优惠券金额  含￥
        money_num[i] = parseInt(discount_money[i].replace(/[^0-9]/ig,""));

        coupon_list[i].index=i;
        if(coupon_list[i].getAttribute("data-effective") == 0){                //过期的劵变灰色
            coupon_list[i].setAttribute("class","item cell-v js_choose_coupon coupon_disable");
        }

        //点击可用优惠券
        coupon_list[i].onclick=function(){
            //判断是否过期  0过期，1为有效
            var s = this.getAttribute("data-effective");
            if(s == 0){
                no_coupon();
            }else if(s == 1){
                no_ul_span.setAttribute("class","vou-radio js_select_dot");
                for(var i=0;i<coupon_list.length;i++){
                    coupon_btn_span[i].setAttribute("class","vou-radio js_select_dot");
                }
                $("#detail_feiyong").find(".no3").eq(2).text("-"+discount_money[this.index]);
                coupon_btn_span[this.index].setAttribute("class","vou-radio js_select_dot current");
                coupon_text.innerHTML = "优惠" + discount_money[this.index];


                $("#js_paybar_wrap").find("p").eq(1).css("display","block");
                $("#js_paybar_wrap").find("p").eq(1).text("优惠已减"+discount_money[this.index]);
                final_money = room_price*get_between - money_num[this.index];
                redpack_money = money_num[this.index];
                paybar_price.innerHTML= final_money;   //相减
            }

        };

        //点击不用优惠券
        no_ul_li.onclick=function(){
            for(var i=0;i<coupon_list.length;i++){
                coupon_btn_span[i].setAttribute("class","vou-radio js_select_dot");
            }
            no_ul_span.setAttribute("class","vou-radio js_select_dot current");
            no_coupon();
        }
    }
}



//获取红包ID
$(".redpack").click(function () {
    redpack_id = $(this).attr('data-id');
    $("#redpack_id").val(redpack_id);

});

//产看费用详细
detail_money_btn.onclick = function () {
    detail_money_page.style.display = "block";

    $("#detail_feiyong").find(".cell-star.total").find("p").text(get_between + "晚" + rooms + "间    " + "¥" + final_money);
    $("#detail_feiyong").find(".no1").eq(0).text(get_intime);
    $("#detail_feiyong").find(".no1").eq(1).text(get_outtime);
    $("#detail_feiyong").find(".no3").eq(0).text(rooms + "x" + room_price);
    $("#detail_feiyong").find(".no3").eq(1).text("x" + get_between);
    detail_money_page.onclick = function () {
        this.style.display = "none";
    }
}
//去支付
function fuck(){
    //判断信息是否完整
    //判断姓名
    var all_ok = [];  //所有名字是否正确
    var username = /^[\u4E00-\u9FA5A-Za-z]+$/;
    name_box_ul.find('input').each(function(i){
        if(!username.test($(this).val())){
            $(this).css('border','1px red solid').attr('is_ok','0');
            console.log("只能中文或英文");
        }else{
            $(this).css('border','').attr('is_ok','1');
        }
        all_ok[i] = $(this).attr('is_ok');   //保存正确率
    });

    name_box_ul.find('input').focus(function(){
        $(this).css('border','');
    })
    $('.js_passenger_phone').find('input').eq(1).focus(function(){
        $(this).css('border','');
    })

    //判断电话
    var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
    var phone = $('.js_passenger_phone').find('input').eq(1).val();
    if(!myreg.test(phone)){
        $('.js_passenger_phone').find('input').eq(1).css('border','1px red solid').attr('is_ok','0');
        console.log("手机号码格式错误");
    }else{
        $('.js_passenger_phone').find('input').eq(1).attr('is_ok','1').css('border','');
    }

    var phone_ok = $('.js_passenger_phone').find('input').eq(1).attr('is_ok');   //电话号码

    var name_ok = true;
    //判断跳转   名字&号码
    if(phone_ok == 1){
        for(var i=0;i<all_ok.length;i++){
            if(all_ok[i] != 1){
                close_tip_page('用户名');
                name_ok = false;
                return;
            }
        }
        //跳转
        if(name_ok){
            $("#final_money").val(final_money);
            $("#get_intime").val(get_intime);
            $("#get_outtime").val(get_outtime);
            $("#all_price").val(all_money);
            $("#order_fj_num").val(rooms);
            $("#redpack_money").val(redpack_money);
            var url = 'hotel.php?mod=pay&house_id='+house_id+'&indate='+get_intime+'&out' +
                'date='+get_outtime;
            $(".pay").attr('action',url);
            $(".pay").submit();
        }

    }else{
        for(var i=0;i<all_ok.length;i++){
            if(all_ok[i] != 1){
                close_tip_page('用户名和手机号码');
                return;
            }else{
                close_tip_page('手机号码');
            }
        }
    }

}

//拦截安卓回退的虚拟按钮
history.pushState(null, null, location.href);
window.addEventListener('popstate', function(event) {
    history.pushState(null, null, location.href );   //禁止安卓返回键
    //此处加入回退时你要执行的代码

    //退出页面提示
    page_mask.style.display="block";
    exit_page.style.display="block";
    //继续
    continue_btn.onclick=function(){
        close_all();
    }
    /*离开*/
    exit_btn.onclick=function(){
        self.location="hotel.php?mod=index";
    }
});


/*离开*/
exit_btn.onclick = function () {
    self.location = "hotel.php?mod=index";
}






//预定早餐套餐
$('.m-list').find('#js_book_zao').on('click',function(){

    $(this).parent().find('.book_zao_list').css({'height':'auto','opacity':'1'});
    $(this).parent().find('.book_zao_list li').on('click',function(i){
        //判断是否为会员价   data-huiyuan = 0 非会员      1会员
        var data_huiyuan = $(this).attr('data-huiyuan');
        if(data_huiyuan == 0){
            $(this).parent().parent().find('.book_zao_list').css({'height':'0','opacity':'0'});
            var zao_text = $(this).find('.m-label').text();
            $(this).parent().parent().find('#js_book_zao').find('#js_booking_invoiceTitle').text(zao_text);
            zao_money =parseInt( $(this).find('.zao_money').text());    //含早的价钱
        }else if(data_huiyuan == 1){
            $(this).parent().parent().find('.book_zao_list').css({'height':'0','opacity':'0'});
            var zao_text = $(this).find('.m-label').text();
            $(this).parent().parent().find('#js_book_zao').find('#js_booking_invoiceTitle').text(zao_text);
            zao_money =parseInt( $(this).find('.huiyuan_money').text());    //含早的价钱
        }

        room_price = zao_money;
        no_coupon();

        $('#detail_feiyong').find('.bd .cost-list .no2').eq(0).text(zao_text);
        $('#initial_price').html(zao_text);

        var id = $(this).attr("data-id");
        if(id==0){
            $("#is_have_bre").val(0);
        }else{
            $("#is_have_bre").val(1);
        }


    })
});


//绑定用户信息错误html
function create_defale_tip_page(text){
    var str = '<div class="tip_false_box_mask"></div>'+
        '<div class="box_content">'+
        '<div class="tip_title">提示</div>'+
        '<div class="tip_content">'+text+'有误</div>'+
        '<div class="tip_btn">确定</div>'+
        '</div>';
    return str;
}

//显示 /关闭页面
function close_tip_page(text){
    var str = create_defale_tip_page(text);
    $('.tip_false_box').append(str);

    $('.tip_false_box').find('.tip_btn').on('click',function(){
        $('.tip_false_box').html('');
    })
}


//拦截安卓回退的虚拟按钮
history.pushState(null, null, location.href);
window.addEventListener('popstate', function(event) {
    var coupon_status = $('#modalLarge').attr('class');
    history.pushState(null, null, location.href );   //禁止安卓返回键
    if(coupon_status.indexOf('in') == -1){   //不在优惠页面
        //此处加入回退时你要执行的代码
        //退出页面提示
        page_mask.style.display="block";
        exit_page.style.display="block";
        //继续
        continue_btn.onclick=function(){
            close_all();
        }
        /*离开*/
        exit_btn.onclick=function(){
            self.location="hotel.php?mod=index";
        }
    }else{
        $('#modalLarge').find('.btn.btn-link.btn-lg').click();
    }

});